
package voteservice;

import java.io.*;
import java.sql.*;


public class VoteImpl implements VoteIF {
    
	/**
	 * Compare whether two roles are in the same branch of the role hierarchy.
	 * 
	 * @param posTargetRole			Position of target role in company's role hierarchy.
	 * @param posPerspectiveRole	Position of perspective role in company's role hierarchy.
	 * @return						true if target role and perspective role are in the same 
	 * 								branch of the role hierarchy.
	 */
	public boolean compareHierarchy(int posTargetRole, int posPerspectiveRole){
		//cast from int to String, which is easy to manipulate
		String posTargetRoleString = String.valueOf(posTargetRole);
		String posPerspectiveRoleString = String.valueOf(posPerspectiveRole);

		//set the prefix by the non-zero substring of target role. 
		//If two roles have the same prefix, that means they are in the same branch in the 
		//role hierarchy.
		int idx = posTargetRoleString.indexOf("0");
		String prefixTargetRole = posTargetRoleString.substring(0, idx);
		String prefixPerspectiveRole = posPerspectiveRoleString.substring(0, idx);
		int result= prefixTargetRole.compareTo(prefixPerspectiveRole);

		if(result==0)
			return true;
		else
			return false;
		
	}


	
	/* (non-Javadoc)
	 * @see voteservice.VoteIF#vote(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean vote(String roleName, String EPCClass, String comId, String serviceProviderComId)
	{
	
		//define display string
		String disp="Making vote decision...";
		
		//define the driver to use 
	      String driver = "org.apache.derby.jdbc.ClientDriver";
	    //the database name  
	      String dbName="Comdb"+serviceProviderComId;
//	      System.out.println("the dbName is: "+dbName);
//	      disp+=("the dbName is: "+dbName+"\n");
	    //define the Derby connection URL to use 
	      String connectionURL = "jdbc:derby://localhost:1527/" + dbName;
//	      System.out.println("the connectionURL is: "+connectionURL);
//	      disp+=("the connectionURL is: "+connectionURL+"\n");
	    
	    
	      Connection conn = null;
	      Statement s;
	      ResultSet rs;
	      ResultSet rs1;
	      boolean result=false;
	      
	      int posGrantedRole=0;
	      int posTargetRole=0;
	    //load derby driver
	      try{
	          Class.forName(driver); 
//	          System.out.println(driver + " loaded. ");
//	          disp+=(driver + " loaded. \n");
	      } catch(java.lang.ClassNotFoundException e)     {
	          System.err.print("ClassNotFoundException: ");
	          System.err.println(e.getMessage());
	          System.out.println("\n    >>> Please check your CLASSPATH variable   <<<\n");
	      }
	      
		
		try{
			conn = DriverManager.getConnection(connectionURL);
//			System.out.println("Connected to database " + dbName);
//			disp+=("Connected to database " + dbName+"\n");
			
			s=conn.createStatement();
			
			//construct sql: get granted role's position according to comId and EPCClass
			String sqlGrantedRolePosition = "SELECT position FROM roleHierarchy WHERE roleName=(SELECT roleName from roleGrant WHERE lid="+comId+"AND EPCClass='"+EPCClass+"')";
			//construct sql: get target role's position
			String sqlTargetRolePosition = "SELECT position FROM roleHierarchy WHERE roleName='"+roleName+"'";

			
			rs = s.executeQuery(sqlGrantedRolePosition);
			
			while(rs.next()){
				posGrantedRole=rs.getInt("position");
			}
			//check is the ResultSet is empty
			if(posGrantedRole==0)
			{
//				System.out.println("There is no role granted to Comany: "+comId);
				disp+=("There is no role granted to Comany: "+comId+"\n");
				System.out.println(disp);
				conn.close();
				return false;
			}else{
//				System.out.println("Position of the role granted to Company "+comId+" is: "+String.valueOf(posGrantedRole));
				disp+=("Position of the role granted to Company "+comId+" is: "+String.valueOf(posGrantedRole)+"\n");
			}
			

			rs1=s.executeQuery(sqlTargetRolePosition);
			while(rs1.next()){
			posTargetRole = rs1.getInt("position");
			}
			//check if the ResultSet is empty
			if(posTargetRole==0){
//				System.out.println("There is no Target Role defined as: "+roleName);
				disp+=("There is no Target Role defined as: "+roleName+"\n");
				System.out.println(disp);
				return false;
			}else{
//				System.out.println("Position of the target role is:"+String.valueOf(posTargetRole));
				disp+=("Position of the target role is:"+String.valueOf(posTargetRole)+"\n");
			}
			
			//check whether the target role and the granted role are in the same branch of role hierarchy
			result = compareHierarchy(posTargetRole, posGrantedRole);
			disp+="Voting result is "+result+"\n";
			//close connection
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(disp);
		return result;
	}
}
